package com.jz.community.moduleshoppingguide.nearshop.ui.activity;


import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.moduleshoppingguide.R;
import com.orhanobut.logger.Logger;

import butterknife.ButterKnife;


public class NearShopInfoLocationActivity extends Activity implements LocationSource, AMapLocationListener {

    private MapView mapView;
    private AMap aMap;
    private UiSettings mUiSettings;
    private CameraUpdate mUpdata;
    Marker marker;
    private String Lon, Lat, nearShopName, nearShopAddress;
    private TextView titleName, tv_location_near_shop_name, tv_location_near_shop_address;
    private ImageView titleBack;
    Toolbar nearShopLocationToolbar;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.module_shoppingguide_activity_near_shop_info_location);
        ButterKnife.bind(this);
        mapView = (MapView) findViewById(R.id.near_shop_location_map);
        mapView.onCreate(savedInstanceState);// 必须要写
        Lon = getIntent().getStringExtra("lon");//经度
        Lat = getIntent().getStringExtra("lat");
        nearShopName = getIntent().getStringExtra("near_shop_name");
        nearShopAddress = getIntent().getStringExtra("near_shop_address");
        initView();

        Logger.i("经纬度:" + Lon + Lat);
        init(Lon, Lat);
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initView() {

        titleName = (TextView) findViewById(R.id.near_shop_location_title);
        titleBack = (ImageView) findViewById(R.id.near_shop_location_back);
        nearShopLocationToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.near_shop_location_toolbar);
        ImmersionBar
                .with(this)
                .titleBar(nearShopLocationToolbar)
                .statusBarDarkFont(true, 0.2f)//当白色背景状态栏遇到不能改变状态栏字体为深色的设备时，解决方案
                .init();
        tv_location_near_shop_name = (TextView) findViewById(R.id.tv_location_near_shop_name);
        tv_location_near_shop_address = (TextView) findViewById(R.id.tv_location_near_shop_address);
        if (!RxDataTool.isNullString(nearShopName)) {
            tv_location_near_shop_name.setText(nearShopName);
            titleName.setText("店铺地址");
        }
        if (!RxDataTool.isNullString(nearShopAddress)) {
            tv_location_near_shop_address.setText(nearShopAddress);
        }

        titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 初始化AMap对象
     */
    private void init(String lon, String lat) {
        if (aMap == null) {
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
            mUiSettings.setZoomControlsEnabled(true);
            mUiSettings.setCompassEnabled(true);
            aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
            aMap.getUiSettings().setScrollGesturesEnabled(false);//设置是否滑动（平移）
            aMap.setMyLocationEnabled(false);
            mUiSettings.setCompassEnabled(false);//隐藏指南针
            mUpdata = CameraUpdateFactory.newCameraPosition(
//15是缩放比例，0是倾斜度，30显示比例
                    new CameraPosition(new LatLng(Double.parseDouble(lat), Double.parseDouble(lon)), 15, 0, 30));//这是地理位置，就是经纬度。
            aMap.moveCamera(mUpdata);//定位的方法
//            drawMarkers(lat, lon);
        }
    }

    public void drawMarkers(String Lat, String Lon) {

        Marker marker = aMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.parseDouble(Lat), Double.parseDouble(Lon)))
                .title("八维")
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .draggable(true));
        marker.showInfoWindow();// 设置默认显示一个infowinfow
    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }


    @Override
    public void onLocationChanged(AMapLocation amapLocation) {

    }

    @Override
    public void activate(OnLocationChangedListener arg0) {

    }

    @Override
    public void deactivate() {

    }


}
